package com.newcapec.modules.terminal.controller;

import java.util.Arrays;
import java.util.Map;

import cn.hutool.crypto.SecureUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.newcapec.modules.terminal.entity.MqttUserEntity;
import com.newcapec.modules.terminal.service.MqttUserService;
import com.newcapec.common.utils.PageUtils;
import com.newcapec.common.utils.R;

/**
 *
 *
 * @author taoge
 * @email 3091818118@qq.com
 * @date 2021-12-16 09:20:54
 */
@RestController
@RequestMapping("terminal/mqttuser")
public class MqttUserController {
    @Autowired
    private MqttUserService mqttUserService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("terminal:mqttuser:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = mqttUserService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("terminal:mqttuser:info")
    public R info(@PathVariable("id") Integer id){
		MqttUserEntity mqttUser = mqttUserService.getById(id);

        return R.ok().put("mqttUser", mqttUser);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("terminal:mqttuser:save")
    public R save(@RequestBody MqttUserEntity mqttUser){
        mqttUser.setUsername(SecureUtil.md5(mqttUser.getUsername()));
		mqttUserService.save(mqttUser);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @RequiresPermissions("terminal:mqttuser:update")
    public R update(@RequestBody MqttUserEntity mqttUser){
		mqttUserService.updateById(mqttUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @RequiresPermissions("terminal:mqttuser:delete")
    public R delete(Integer[] ids){
		mqttUserService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
}
